//
//  ContactsView.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

struct ContactsView: View {
    @EnvironmentObject var contactsViewModel: ContactsViewModel
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                // 搜索栏
                SearchBar(text: $contactsViewModel.searchText)
                    .padding(.horizontal)
                    .padding(.top, 8)
                
                // 功能菜单
                ContactsMenuView()
                
                Divider()
                
                // 联系人列表
                ContactsListView()
                    .environmentObject(contactsViewModel)
            }
            .navigationTitle("通讯录")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        // 添加朋友功能
                    }) {
                        Image(systemName: "person.badge.plus")
                    }
                }
            }
        }
    }
}

struct ContactsMenuView: View {
    var body: some View {
        VStack(spacing: 0) {
            ContactsMenuItemView(
                icon: "person.2.fill",
                title: "新的朋友",
                badge: 2
            ) {
                print("新的朋友")
            }
            
            Divider()
                .padding(.leading, 60)
            
            ContactsMenuItemView(
                icon: "person.3.fill",
                title: "群聊",
                badge: nil
            ) {
                print("群聊")
            }
            
            Divider()
                .padding(.leading, 60)
            
            ContactsMenuItemView(
                icon: "tag.fill",
                title: "标签",
                badge: nil
            ) {
                print("标签")
            }
            
            Divider()
                .padding(.leading, 60)
            
            ContactsMenuItemView(
                icon: "square.and.pencil",
                title: "公众号",
                badge: nil
            ) {
                print("公众号")
            }
        }
        .background(Color(UIColor.systemBackground))
    }
}

struct ContactsMenuItemView: View {
    let icon: String
    let title: String
    let badge: Int?
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            HStack(spacing: 15) {
                Image(systemName: icon)
                    .font(.system(size: 20))
                    .foregroundColor(.green)
                    .frame(width: 30, height: 30)
                
                Text(title)
                    .font(.system(size: 16))
                    .foregroundColor(.primary)
                
                Spacer()
                
                if let badge = badge, badge > 0 {
                    Text("\(badge)")
                        .font(.system(size: 12, weight: .medium))
                        .foregroundColor(.white)
                        .frame(minWidth: 18, minHeight: 18)
                        .background(Color.red)
                        .clipShape(Circle())
                }
                
                Image(systemName: "chevron.right")
                    .font(.system(size: 14))
                    .foregroundColor(.secondary)
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 12)
        }
        .buttonStyle(PlainButtonStyle())
    }
}

struct ContactsListView: View {
    @EnvironmentObject var contactsViewModel: ContactsViewModel
    
    var body: some View {
        List {
            ForEach(contactsViewModel.sortedSectionKeys, id: \.self) { sectionKey in
                if let contacts = contactsViewModel.groupedContacts[sectionKey] {
                    Section(header: Text(sectionKey)) {
                        ForEach(contacts) { contact in
                            NavigationLink(destination: ContactDetailView(contact: contact)) {
                                ContactRowView(contact: contact)
                            }
                        }
                    }
                }
            }
        }
        .listStyle(PlainListStyle())
    }
}

struct ContactRowView: View {
    let contact: User
    
    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: contact.avatar)
                .font(.system(size: 35))
                .foregroundColor(.blue)
                .frame(width: 45, height: 45)
                .background(Color.gray.opacity(0.2))
                .clipShape(RoundedRectangle(cornerRadius: 8))
            
            VStack(alignment: .leading, spacing: 2) {
                Text(contact.displayName)
                    .font(.system(size: 16, weight: .medium))
                    .foregroundColor(.primary)
                
                if !contact.signature.isEmpty {
                    Text(contact.signature)
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                        .lineLimit(1)
                }
            }
            
            Spacer()
            
            if contact.isOnline {
                Circle()
                    .fill(Color.green)
                    .frame(width: 8, height: 8)
            }
        }
        .padding(.vertical, 2)
    }
}

struct SearchBar: View {
    @Binding var text: String
    
    var body: some View {
        HStack {
            Image(systemName: "magnifyingglass")
                .foregroundColor(.secondary)
            
            TextField("搜索", text: $text)
        }
        .padding(.horizontal, 12)
        .padding(.vertical, 8)
        .background(Color(.systemGray6))
        .cornerRadius(10)
    }
}
